from rest_framework.views import APIView
from rest_framework import serializers
from rest_framework import status
from rest_framework.response import Response
from api import models

class NewsFaoverSerializer(serializers.ModelSerializer):
    class Meta:
        model=models.NewsFavorRecord
        fields=['news',]

class NewsFavorRecord(APIView):
    def post(self,request):
        ser=NewsFaoverSerializer(data=request.data)

        if ser.is_valid():
            news_obj=ser.validated_data.get('news')
            newsfavorrecord_obj=models.NewsFavorRecord.objects.filter(news=news_obj)
            if newsfavorrecord_obj:
                newsfavorrecord_obj.delete()
                return Response({}, status=status.HTTP_200_OK)
            else:
                models.NewsFavorRecord.objects.create(user=request.user,news=news_obj)
                return Response({}, status=status.HTTP_201_CREATED)

